﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ChapterX.BusinessFacade;
using ChapterX.Common;
using ChapterX.Data.BooksDSTableAdapters;

namespace ChapterX.Auth.Teacher
{
    public partial class ApplicationDetail : ChapterX.Model.PageBase
    {
        protected int DigiStatus;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int bookId;

                SetDigiOperation();

                if (Utils.HasValue(Request.QueryString[QueryKeys.BOOK_ID]) &&
                    Int32.TryParse(Utils.GetValue(Request.QueryString[QueryKeys.BOOK_ID]), out bookId))
                {
                    this.hl_GoDigitizing.NavigateUrl = String.Format("../Librarian/ChapterList.aspx?{0}={1}", QueryKeys.BOOK_ID, bookId);
                    this.hf_BookId.Value = bookId.ToString();

                    DigiStatus = (new DigitizationSystem()).GetDigitizationStatus(bookId);
                    SetAccessibilityLink(bookId, DigiStatus);
                }
            }
        }

        protected void SetDigiOperation()
        {
            //Librarian有权限操作，Teacher没有
            if (CurrentUserInfo.IsTeacher)
            {
                this.btn_DenyApp.Visible = false;
                this.hl_GoDigitizing.Visible = false;
            }
            else
            {
                this.btn_DenyApp.Visible = true;
                this.hl_GoDigitizing.Visible = true;
            }
        }

        protected void SetAccessibilityLink(int bookId, int digStatus)
        {
            //图书必须与课程绑定，且书已数字化，才可设置访问权限
            BookSystem bs = new BookSystem();

            if (digStatus == (int)CommonDefinition.DigitizationStatus.digitized)
            {
                if (bs.HasCourses(bookId))
                {
                    this.hl_SetAccessibility.Visible = true;
                    this.hl_SetAccessibility.NavigateUrl = String.Format("SetAccessibility.aspx?{0}={1}", QueryKeys.BOOK_ID, bookId);
                }
                else
                {
                    
                    this.hl_SetAccessibility.Visible = false;
                    this.l_Tip.Visible = true;
                    this.l_Tip.Text = "，但没有绑定课程，图书必须绑定课程，才能设置访问权限";
                }
            }
            else 
            {
                this.hl_SetAccessibility.Visible = false;
            }
        }

        protected void btn_DenyApp_Click(object sender, EventArgs e)
        {
            try
            {
                int bookId;

                bookId = Int32.Parse(this.hf_BookId.Value);

                DigitizationSystem ds = new DigitizationSystem();
                bool result = ds.UpdateDigitizationStatus(bookId, (int)CommonDefinition.DigitizationStatus.denied);

                if (result)
                    SetInfoMessage("拒绝申请成功");
                else
                    SetErrorMessage("拒绝申请失败");
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                SetErrorMessage("拒绝申请失败");
            }
        }
    }
}
